﻿using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
using SIG.Model.Case;

namespace SIG.Data.Mapping
{
    public class WorkMap : EntityTypeConfiguration<Work>
    {
        public WorkMap()
        {
            this.HasKey(p => p.Id);
            this.Property(p => p.Id)
                .HasColumnName("Id")
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

            this.ToTable("WorkSet");

            this.Property(p => p.Title).HasMaxLength(100).IsRequired();  
        
            this.Property(p => p.ViewCount).IsRequired();
            this.Property(p => p.CreatedDate).IsRequired().HasColumnType("datetime");
            this.Property(p => p.CreatedBy).HasMaxLength(50);
            this.Property(p => p.UpdatedDate).IsOptional().HasColumnType("datetime");
            this.Property(p => p.UpdatedBy).IsOptional().HasMaxLength(50);

            this.HasMany(a => a.WorkTypes).WithMany(b => b.Works).Map(m =>
            {
                m.MapLeftKey("WorkId");
                m.MapRightKey("TypeId");
                m.ToTable("Work_Types");
            });


            this.HasRequired(l => l.Solution)
            .WithMany(l => l.Works)
            .HasForeignKey(l => l.SolutionId);

            this.HasOptional(l => l.Client)
            .WithMany(l => l.Works)
            .HasForeignKey(l => l.ClientId);

        }
    
    }
}
